<?php
if($_POST['ed_przyp_typ']) $_SESSION['filtr_typ']=$_POST['ed_przyp_typ'];
else $_SESSION['filtr_typ']=100;
if($_POST['ed_przyp_firma']<>'') $_SESSION['filtr_firma']=$_POST['ed_przyp_firma'];
else $_SESSION['filtr_firma']=100;

if ($_POST['bad_wolne'] && $_POST['bad_dod']){
	foreach ($_POST['bad_wolne'] as $i=>$w){
		$sql="insert into gab_bad values (".$_SESSION['ed_lek_bad'].", $w)";
		mysql_query($sql);
		logg($sql, $_SESSION['u']);
	}
}

if ($_POST['bad_zaj'] && $_POST['bad_usun']){
	foreach ($_POST['bad_zaj'] as $i=>$w){
		$sql="delete from  gab_bad where id_gab =".$_SESSION['ed_lek_bad']." and id_bad = ".$w;
		mysql_query($sql);
		logg($sql, $_SESSION['u']);
	}
}

if ($_POST['ed_lek_bad']) $_SESSION['ed_lek_bad']=$_POST['ed_lek_bad'];

$sql="select id_bad from gab_bad where id_gab=".$_SESSION['ed_lek_bad'];
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
	$list_gab_bad[]=$row[0];
}

$gabi_lek=@implode(" ,", $list_gab_bad);

$sql41="select * from sl_firmy";
$result41=mysql_query($sql41);
	echo '<style tyle=text/css>';
	while($row41 = mysql_fetch_array($result41))
	{
		echo '.opt'.$row41['id'].' {background-color: #'.$row41['kolor'].';} ';
	}
	echo '</style>';
 

	$sql11="select sl_uslugi.* from sl_uslugi where akt=1 ";
	if ($gabi_lek<>'') $sql11=$sql11." and sl_uslugi.id not in (".$gabi_lek.")";
        if ($_SESSION['filtr_firma']<>'100') $sql11=$sql11." and sl_uslugi.id_firmy = ".$_SESSION['filtr_firma'];
        if ($_SESSION['filtr_typ']<>'100') $sql11=$sql11." and sl_uslugi.id_grupy = ".$_SESSION['filtr_typ'];
	$sql11=$sql11." order by nazwa, id_firmy, id_grupy";
	#echo $sql11;
	$result11=mysql_query($sql11);
	while($row11 = mysql_fetch_array($result11))
	{
		$list_typ_wizz[$row11[0]]=$row11['nazwa'];
		$list_typ_wizz_kolor[$row11[0]]=$row11['id_firmy'];
	}
	if ($gabi_lek<>''){
	
	$sql12="select * from sl_uslugi where akt=1 ";
	if ($gabi_lek<>'') $sql12=$sql12." and id in (".$gabi_lek.")";
	if ($_SESSION['filtr_firma']<>'100') $sql12=$sql12." and sl_uslugi.id_firmy = ".$_SESSION['filtr_firma'];
        if ($_SESSION['filtr_typ']<>'100') $sql12=$sql12." and sl_uslugi.id_grupy = ".$_SESSION['filtr_typ'];
        $sql12=$sql12." order by nazwa, id_firmy, id_grupy";
	#echo $sql12;
	$result12=mysql_query($sql12);
	while($row12 = mysql_fetch_array($result12))
	{
		$list_typ_wiz_lek[$row12['id']]=$row12['nazwa'];
		$list_typ_wiz_kolor2[$row12['id']]=$row12['id_firmy'];
	}
	}
        
        $list_typ_bad=array(100=>'Wszystkie',1=>"Konsultacja", 2=>'Badanie', 3=>'Zestaw');
        $sql_firmy="select * from view_firmy";
        $result_firmy=mysql_query($sql_firmy);
        while ($row_firmy = mysql_fetch_array($result_firmy)){
            $lista_firm[$row_firmy[0]]=$row_firmy[1];
        }
        
?>


<form action="index.php" method="POST"><input type=submit value=Anuluj class="butt4"></form>

<form action="index.php" method="POST"><input type=hidden name=dod_usl value=1><input type=submit value="Dodaj nową usługę" class="butt"></form>

<form class="fancy" id="fancy" method="post" action="">
 <fieldset >
   <legend>Edycja usług dla lekarza <?php echo get_name('sl_gab', $_SESSION['ed_lek_bad']); ?></legend>
   <table align="center">
       <tr class="linia">
           <td class="linia">Typ:</td>
           <td class="linia">Płatnik:</td>
           <td rowspan="2"><input type="submit" value="Szukaj" name="filtr_badania" class="butt4"></td>
       </tr>
       <tr class="linia">
           <td class="linia"><?php form_auto_submit_2m("ed_przyp_typ", $list_typ_bad, $_SESSION['filtr_typ']); ?></td>
           <td class="linia"><?php form_auto_submit_2m("ed_przyp_firma", $lista_firm, $_SESSION['filtr_firma']); ?></td>
       </tr>
   
   </table>
   <table align="center"><tr class="linia"><td class="linia">
   <select class="required" name="bad_wolne[]" multiple="multiple" id="uslugi" size="15">
   <?php 
   
     foreach ($list_typ_wizz as $i=>$w){
     	
     	echo '<option value="'.$i.'"';
     	echo ' class=opt'.$list_typ_wizz_kolor[$i].'>'.$w.'</option>';
     	
     }  
     ?>
   </select>
   </td><td class="linia">
   <input class="submit" name="bad_dod" type="submit" value=">>"/><br>
   <input class="submit" name="bad_usun" type="submit" value="<<" />
   </td><td class="linia">
   <select class="required" name="bad_zaj[]" multiple="multiple" id="uslugi" size="15">
   <?php 
   unset($i,$w);
     foreach ($list_typ_wiz_lek as $i=>$w){
     	
     	echo '<option value="'.$i.'"';
     	echo ' class=opt'.$list_typ_wiz_kolor2[$i].'>'.$w.'</option>';
     	
     }  
     ?>
   </select>
   </td>
   </tr>
   <tr><td colspan=3 align="center">
   <input type=submit value="Zapisz">
   </td></tr>
   </table>
 </fieldset>
</form>